package com.yanqu.road.server.gameplayer.module.player.beauty;

import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.config.skill.SkillUpgradeInfo;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserBeauty;
import com.yanqu.road.entity.travelbook.UserTravelAlbum;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.travelbook.TravelBookModule;
import com.yanqu.road.server.gameplayer.module.player.BeautyModule;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.config.TravelBookConfigMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @Author : Cxp
 * @Time : 2024-11-19
 * @Description : 旅行册卡册技能升级
 */

public class TravelBookAlbumSkill extends BeautyUpgradeSkillAction {

    public TravelBookAlbumSkill(int beautyId, int position, int skillType, int batch, BeautyModule beautyModule,
                                GamePlayer player) {
        super(beautyId, position, skillType, batch, beautyModule, player);
    }

    @Override
    public void setOldLv() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.TravelBook.getValue())) {
            this.ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        int skillId = position;
        SkillInfo skillInfo = SkillMgr.getSkillInfo(skillId);
        if (skillInfo == null) {
            this.ret = GameErrorCode.E_BEAUTY_PATRONS_SKILL_ERR;
            return;
        }
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        if (userBeauty == null) {
            this.ret = GameErrorCode.E_BEAUTY_NO_FOUND;
            return;
        }
        Map<Integer, Integer> albumSkillMap = userBeauty.getAlbumSkillMap();
        oldLv = albumSkillMap.get(skillId);
        if (!albumSkillMap.containsKey(skillId)) {
            this.ret = GameErrorCode.E_TRAVEL_BOOK_ALBUM_SKILL_LOCK; // 游册技能未解锁
        }
    }

    @Override
    public SkillInfo getSkillInfo() {
        int skillId = position;
        return SkillMgr.getSkillInfo(skillId);
    }

    @Override
    public void calConsumer() {
        SkillInfo skillInfo = getSkillInfo();
        if (skillInfo == null) {
            return;
        }
        int skillId = skillInfo.getId();
        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        if (userBeauty == null) {
            this.ret = GameErrorCode.E_BEAUTY_NO_FOUND;
            return;
        }
        Map<Integer, Integer> albumSkillMap = userBeauty.getAlbumSkillMap();
        if (!albumSkillMap.containsKey(skillId)) {
            this.ret = GameErrorCode.E_TRAVEL_BOOK_ALBUM_SKILL_LOCK;
            return;
        }

        int maxSkillLv = skillInfo.getMaxLevel(); // 技能原有最大等级
        // 旅行册卡册等级增加技能等级上限
        List<UserTravelAlbum> albums = player.getModule(TravelBookModule.class).getBeautyAlbumMap().get(beautyId);
        if (albums != null) {
            for (UserTravelAlbum album : albums) {
                int albumId = album.getAlbumId();
                maxSkillLv += TravelBookConfigMgr.getSkillExtendLevel(albumId, album.getLevel(), getSkillInfo().getId());
            }
        }


        int skillLv = userBeauty.getAlbumSkillMap().get(skillId);
        if (skillLv >= maxSkillLv) {
            this.ret = GameErrorCode.E_TRAVEL_BOOK_ALBUM_SKILL_MAX_LEVEL;
            return;
        }

        int upAble = Math.min(this.batch, maxSkillLv - skillLv);

        // 计算消耗
        long totalCost = 0;
        for (int i = 0; i < upAble; i++) {
            // 当前等级需要的消耗
            SkillUpgradeInfo consume = SkillMgr.getSkillUpgradeInfo(skillInfo.getUpgradeType(0), this.oldLv + i);
            if (consume == null) {
                break;
            }
            if (userBeauty.getSkillExp() < totalCost + consume.getConsumeCount()) { // 道具不足
                break;
            } else {
                totalCost += consume.getConsumeCount();
                this.realBatch++;
            }
        }

        this.needConsume = totalCost;
        if(this.needConsume > 0){
            this.consumer = new Property(GameConfig.GAME_MONEY_BEAUTY_EXP, BigInteger.valueOf(this.needConsume));
        }
        if (this.realBatch <= 0) {
            this.ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }
    }

    @Override
    public void upgrade() {
        preValidArgument();

        if (this.ret == 0) {
            checkMaxLv();
        }

        if (this.ret == 0) {
            calConsumer();
        }

        UserBeauty userBeauty = beautyModule.getUserBeauty(beautyId);
        if (userBeauty.getSkillExp() < this.needConsume) {
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
        }

        if (this.ret == 0) {
            userBeauty.setSkillExp(userBeauty.getSkillExp() - this.needConsume);
            userBeauty.albumSkillLevelUp(getSkillInfo().getId(), this.realBatch);
        }
    }
}
